package com.shockweb.common.serializable.xml;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilderFactory;

import com.shockweb.common.International;
import com.shockweb.common.serializable.SerializableObject;
import com.shockweb.common.utils.ClassUtil;
import com.shockweb.common.utils.StringUtil;
import com.shockweb.common.utils.classmethod.ClassMethod;
import com.shockweb.common.utils.classmethod.ClassMethodDefine;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
 * xml解析类open.xml.XmlReader能将xml字符或文件读取到Pojo值对象中，开发人员只需要几行代码就能完成工作，
 * 而不必了解dom等复杂的xml解析工具类。目前XmlReader能支持java的基本数据类型、List接口、自定义的Pojo及嵌套。
 * XmlReader支持属性方式、描述方式。
 * @author: 彭明华
 * 2017年5月18日  彭明华创建
 *
 */
public class XmlReader
{

    /**
     * null
     */
    public static final String NULL="|null|";
    /**
     * 空字符串
     */
    public static final String EMPTY ="|empty|";
	/**
	 * 使用unicode转码开关
	 */
	private boolean unicode = false;
	
	/**
	 * 设置是否使用unicode转码
	 * @param unicode
	 */
	public void setUnicode(boolean unicode){
		this.unicode = unicode;
	}
	

	/**
     * 构造方法
     * @throws XmlException
     */
    public XmlReader() throws XmlException
    {
        this(false);
    }
    
    /**
     * xml读取器
     * @param unicode
     * @throws XmlException
     */
    public XmlReader(boolean unicode) throws XmlException
    {
    	this.unicode = unicode;
        dbf = DocumentBuilderFactory.newInstance();
    }   
    

    
    /**
     * xmlFile文件读取到rootClass的实例中,rootClass是rootClass中set方法中List和Map的对象描述
     * @param xmlFile xml文件
     * @param rootClass 实例的Class定义
     * @return Object 返回实例
     * @throws XmlException
     */
    public Object readFile(String xmlFile, Class<?> rootClass) throws XmlException
    {
        Object rtn = null;
        FileInputStream f = null; 
        try
        {
        	f = new FileInputStream(xmlFile);
        	rtn = readStream(f,rootClass);
        }
        catch(Exception e)
        {
            throw new XmlException(e);
        }finally{
        	if(f!=null){
        		try {
					f.close();
					f = null;
				} catch (IOException e) {
				}
        	}
        }
        return rtn;
    }

	/**
	 * xmlFile文件读取到rootClass的实例中,rootClassDefine是rootClass中set方法中List和Map的对象描述
	 * @param xmlFile xml文件
	 * @param rootClassDefine 多个Class定义,如：rootClassDefine是rootClass中set方法中List的对象描述
	 * @return Object 返回实例
	 * @throws XmlException
	 */
	public Object readFile(String xmlFile, Class<?>... rootClassDefine) throws XmlException
	{
	    Object rtn = null;
	    FileInputStream f = null; 
	    try
	    {
	    	f = new FileInputStream(xmlFile);
	    	rtn = readStream(f,rootClassDefine);
	    }
	    catch(Exception e)
	    {
	        throw new XmlException(e);
	    }finally{
	    	if(f!=null){
	    		try {
					f.close();
					f = null;
				} catch (IOException e) {
				}
	    	}
	    }
	    return rtn;
	}
    /**
     * 从xmlStr字符串中读取到rootClass的实例中,rootClassDefine是rootClass中set方法中List和Map的对象描述
     * @param xmlStr xml字符串
     * @param rootClass 多个Class定义,如：rootClassDefine是rootClass中set方法中List的对象描述
     * @return Object 返回实例
     * @throws XmlException
     */
    public Object readStringNoTitle(String xmlStr, Class<?>... rootClass)throws XmlException
    {
		if(xmlStr==null || NULL.equals(xmlStr.trim()) || "".equals(xmlStr.trim())){
			return null;
		}  
		StringBuilder sb = new StringBuilder();
    	sb.append(XmlWriter.TITLE).append(xmlStr);
    	try {
    		return readStream(new ByteArrayInputStream(sb.toString().getBytes(International.CHARSET)), rootClass);
		} catch (UnsupportedEncodingException e) {
			throw new XmlException(e);
		}
    }
    


    /**
     * 从xmlStr字符串中读取到rootClass的实例中,rootClassDefine是rootClass中set方法中List和Map的对象描述
     * @param xmlStr xml字符串
     * @param rootClassDefine 多个Class定义,如：rootClassDefine是rootClass中set方法中List和Map的对象描述
     * @return Object 返回实例
     * @throws XmlException
     */
    public Object readString(String xmlStr, Class<?>... rootClassDefine)throws XmlException
    {
        if(xmlStr==null || NULL.equals(xmlStr.trim()) || "".equals(xmlStr.trim())){
            return null;
        }
	    try{
	        return readStream(new ByteArrayInputStream(xmlStr.getBytes(International.CHARSET)), rootClassDefine);
		} catch (UnsupportedEncodingException e) {
			throw new XmlException(e);
		}
    }
    
    /**
     * DOM解析对象
     */
    private DocumentBuilderFactory dbf = null;
    
    /**
     * xmlStream个流读取到rootClass的实例中,rootClassDefine是rootClass中set方法中List和Map的对象描述
     * @param xmlStream xml数据流
     * @param rootClassDefine 实例的Class定义
     * @return Object 返回实例
     * @throws XmlException
     */
    public Object readStream(InputStream xmlStream, Class<?>... rootClassDefine) throws XmlException
    {
        Object rtn = null;
        if(rootClassDefine==null || rootClassDefine.length==0){
            throw new XmlException("rootClassDefine不能为空");
        }
        try
        {
        	Element element = dbf.newDocumentBuilder().parse(xmlStream).getDocumentElement();
    		if(List.class.isAssignableFrom(rootClassDefine[0])){
       			rtn = readListValue(element, rootClassDefine[0],rootClassDefine, null,0,"list",0);
    		}else if(Map.class.isAssignableFrom(rootClassDefine[0])){
    			rtn = readMapValue(element, rootClassDefine[0], rootClassDefine,null,0,"map",0);
    		}else if(isCustomClass(rootClassDefine[0])){
    			rtn = readValue(element, rootClassDefine[0],rootClassDefine, null);
    		}else{
    			String tValue = getValue(element, "value",0);
    			if(tValue!=null){
    				rtn = convertValue(rootClassDefine[0],tValue);
    			}
    			
    		}
        }
        catch(Throwable e)
        {
            throw new XmlException(e);
        }
        return rtn;
    }

	
    private Map<String,List<ClassMethodDefine>> methodses = new HashMap<String,List<ClassMethodDefine>>();

    /**
     * 将value转化成cls的实例
     * @param cls 类定义
     * @param value 原数据
     * @return Object 实例
     * @throws ParseException
     */
    private Object convertValue(Class<?> cls, String value) throws ParseException
    {
    	if(value!=null){
        	if(NULL.equals(value)){
        		return null;
        	}
            if(String.class.equals(cls)){
                if(EMPTY.equals(value)){
                    return "";
                }
            	value = format(value,unicode);
    			return value.replaceAll("\\|\\|","\\|");
            }
            if(StringBuffer.class.equals(cls)){
                if(EMPTY.equals(value)){
                    return new StringBuffer("");
                }
            	return new StringBuffer(format(value,unicode));
            }
            if(StringBuilder.class.equals(cls)){
                if(EMPTY.equals(value)){
                    return new StringBuilder("");
                }
            	return new StringBuilder(format(value,unicode));
            }
            if(BigDecimal.class.equals(cls)){
    			try{
    				return new BigDecimal(value);
    			}catch(Exception e){
    				return new BigDecimal(value.replaceAll(",",""));
    			}
            }if(double.class.equals(cls) || Double.class.equals(cls)){
    			DecimalFormat decParser = new DecimalFormat(International.IN_DECIMAL);
    			return decParser.parse(value).doubleValue();
            }if(float.class.equals(cls) || Float.class.equals(cls)){
    			DecimalFormat decParser = new DecimalFormat(International.IN_DECIMAL);
    			return decParser.parse(value).floatValue();
            }if(int.class.equals(cls) || Integer.class.equals(cls)){
    			DecimalFormat decParser = new DecimalFormat(International.IN_INTEGER);
    			return decParser.parse(value).intValue();
            }if(long.class.equals(cls) || Long.class.equals(cls)){
    			DecimalFormat decParser = new DecimalFormat(International.IN_INTEGER);
    			return decParser.parse(value).longValue();
            }if(short.class.equals(cls) || Short.class.equals(cls)){
    			DecimalFormat decParser = new DecimalFormat(International.IN_INTEGER);
    			return decParser.parse(value).shortValue();
            }if(byte.class.equals(cls) || Byte.class.equals(cls))
                return new Byte(value);
            if(char.class.equals(cls) || Character.class.equals(cls))
                return value.charAt(0);
            if(Date.class.equals(cls))
            {
                SimpleDateFormat sdf = new SimpleDateFormat(International.IN_DATETIME);
                return new Date(sdf.parse(value).getTime());
            }
            if(Time.class.equals(cls))
            {
                SimpleDateFormat sdf = new SimpleDateFormat(International.IN_TIME);
                return new Time(sdf.parse(value).getTime());
            }
            if(Timestamp.class.equals(cls))
            {
                SimpleDateFormat sdf = new SimpleDateFormat(International.IN_DATETIMEMSEL);
                return new Timestamp(sdf.parse(value).getTime());
            }
            if(java.util.Date.class.equals(cls))
            {
                SimpleDateFormat sdf = new SimpleDateFormat(International.IN_DATETIME);
                return sdf.parse(value);
            }
            if(boolean.class.equals(cls) || Boolean.class.equals(cls))
                return new Boolean(value);
            if(byte[].class.equals(cls) || byte[].class.equals(cls))
                return SerializableObject.base64Decode(value);
            if(BigInteger.class.equals(cls)){
    			try{
    				return new BigInteger(value);
    			}catch(Exception e){
    				return new BigInteger(value.replaceAll(",",""));
    			}
            }
    	}
        return null;
    }

    /**
     * 判断是否用户自定义类
     * @param cls 类定义
     * @return boolean 如果是自定义类返回true，否则返回false
     */
    private static boolean isCustomClass(Class<?> cls){
        if(String.class.equals(cls))
            return false;
        if(StringBuffer.class.equals(cls))
            return false;
        if(BigDecimal.class.equals(cls))
            return false;
        if(double.class.equals(cls) || Double.class.equals(cls))
            return false;
        if(float.class.equals(cls) || Float.class.equals(cls))
            return false;
        if(int.class.equals(cls) || Integer.class.equals(cls))
            return false;
        if(long.class.equals(cls) || Long.class.equals(cls))
            return false;
        if(short.class.equals(cls) || Short.class.equals(cls))
            return false;
        if(boolean.class.equals(cls) || Boolean.class.equals(cls))
            return false;
        if(byte.class.equals(cls) || Byte.class.equals(cls))
            return false;
        if(char.class.equals(cls) || Character.class.equals(cls))
            return false;
        if(Date.class.equals(cls))
            return false;
        if(Time.class.equals(cls))
            return false;
        if(Timestamp.class.equals(cls))
            return false;
        if(java.util.Date.class.equals(cls))
            return false;
        if(boolean.class.equals(cls) || Boolean.class.equals(cls))
            return false;
        if(BigInteger.class.equals(cls))
            return false;
        if(StringBuilder.class.equals(cls))
            return false;
        if(byte[].class.equals(cls))
            return false;
        return true;
    }
    

    
    /**
     * 实现NodeList接口，存储Node
     * @author 彭明华
     *
     */
    class NodeLists implements NodeList{
        protected Vector<Node> nodes = new Vector<Node>();
        
        public void add(Node node){
        	nodes.add(node);
        }
        
		public Node item(int index) {
			return nodes.get(index);
		}

		public int getLength() {
			return nodes.size();
		}
    	
    }
    
    /**
     * 根据name查询element下一级的node
     * @param element DOM的Element元素
     * @param name 查询的对象
     * @return NodeList
     */
    public NodeList getElementsCurrByTagName(Element element,String name){
    	NodeLists lists = new NodeLists();
    	NodeList nodes = element.getChildNodes();
    	for(int i=0;i<nodes.getLength();i++){
    		if(nodes.item(i).getNodeName().equals(name)){
    			lists.add(nodes.item(i));
    		}
    	}
    	return lists;
    }
    
    /**
     * 获取Element下一级第index的node的描述
     * @param element DOM的Element元素
     * @param name 查询的对象
     * @param index 位置
     * @return String
     */
    public String getValue(Element element,String name,int index){
    	String value = null;
    	NodeList nodes = element.getChildNodes();
    	int start = 0;
    	for(int i=0;i<nodes.getLength();i++){
    		if(nodes.item(i).getNodeName().equals(name)){
    			if(index == start){
    				Element ele = ((Element)nodes.item(i));
    	        	if(ele.getChildNodes() != null && ele.getChildNodes().getLength() > 0){
	            		Node node = ele.getChildNodes().item(0);
	            		value = node.getNodeValue();
	            		break;
	            	}else{
	            	    value = "";
	            	    break;
	            	}
    			}
    			start ++;
    		}
    	}
    	return value;
    }
    
    /**
     * 读取一个element
     * @param element DOM的Element元素
     * @param classDefine class定义
     * @param rootClassDefine 多个rootClassDefine
     * @param method 方法定义
     * @return Object 返回实例
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     * @throws InvocationTargetException
     * @throws ClassNotFoundException
     * @throws ParseException
     */
    private Object readValue(Element element, Class<?> classDefine,Class<?>[] rootClassDefine,Method method)
        throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException, ParseException
    {
        Object rtn = null;
        if(classDefine == null)
            return null;
        if(element != null)
        {
        	try{
        		if(classDefine.isInterface()){
        			Class<?> newClass = ClassUtil.getClassDefine(method,rootClassDefine,0);
        			if(newClass==null){
        				return null;
        			}
        			rtn = newClass.newInstance();
        		}else{
        			if(classDefine.equals(Object.class)){
        				Class<?> newClass = ClassUtil.getClassDefine(method,rootClassDefine,0);
            			if(newClass!=null){
            				rtn = newClass;
            			}
        			}else{
        				rtn = classDefine.newInstance();
        			}
        		}
        	}catch(InstantiationException e){
        		throw new InstantiationException(classDefine.getName() + e.getMessage());
        	}catch(IllegalAccessException e2){
        		throw new IllegalAccessException(classDefine.getName() + e2.getMessage());
        	}catch(IllegalArgumentException e3){
        		throw new IllegalArgumentException(classDefine.getName(),e3);
        	}
    		if(isCustomClass(classDefine)){
	        	List<ClassMethodDefine> methods = methodses.get(rtn.getClass().getName());
	        	if(methods==null){
	        		methods = ClassMethod.getSetMethods(rtn.getClass());
	        		methodses.put(rtn.getClass().getName(),methods);
	        	}
	            for(ClassMethodDefine setMethod : methods)
	            {
	                Method m = setMethod.getMethod();
	                Object value = null;
	                boolean flag = true;
	                if(List.class.isAssignableFrom(m.getParameterTypes()[0])){
	                    value = readListValue(element, m.getParameterTypes()[0],rootClassDefine, setMethod.getMethod(),0,setMethod.getName(),0);
	                }else if(Map.class.isAssignableFrom(m.getParameterTypes()[0])){
	                	value = readMapValue(element, m.getParameterTypes()[0],rootClassDefine, setMethod.getMethod(),0,setMethod.getName(),0);
	                }else{
	                    if(isCustomClass(m.getParameterTypes()[0])){
	                    	NodeList nodes = getElementsCurrByTagName(element,setMethod.getName());
	                    	for(int i=0;i<nodes.getLength();i++){
	                    		Element e = (Element)nodes.item(i);
	                    		value = readValue(e, m.getParameterTypes()[0],rootClassDefine,setMethod.getMethod());
	                    	}
	                    }else{
	                    	String tValue = getValue(element, setMethod.getName(),0);
	                    	if(tValue == null){
	                    		flag = false;
	                    	}else{
	                    		value = convertValue(m.getParameterTypes()[0],tValue);
	                    	}
	                    }
	                }
	                if(flag){
	                	m.invoke(rtn, new Object[]{value});
	                }else{
	                	return null;
	                }
	            }
        	}else{
            	rtn = convertValue(classDefine,getValue(element, "value",0));
        	}
        }
        return rtn;
    }
    
    /**
     * 读取list中的值
     * @param element DOM的Element元素
     * @param classDefine class定义
     * @param rootClassDefine 多个rootClassDefine
     * @param method 方法定义
     * @param index class的索引
     * @param name Element的名称
     * @param valueIndex 读取值的索引
     * @return Object 返回实例
     * @throws IllegalArgumentException
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws ClassNotFoundException
     * @throws ParseException
     */
	@SuppressWarnings("unchecked")
	private List<Object> readListValue(Element element, Class<?> classDefine,Class<?>[] rootClassDefine,Method method,int index,String name,int valueIndex)
        throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, ParseException
    {
        List<Object> rtn = null;
        List<Object> list = null;

        if(classDefine.isInterface()){
        	Class<?> newClass = ClassUtil.getClassDefine(method,rootClassDefine,index);
			if(newClass!=null && !newClass.isInterface() && List.class.isAssignableFrom(newClass)){
				list = (List<Object>)newClass.newInstance();
			}else {
				list = new ArrayList<Object>();
			}
        }else{
        	List<Object> newInstance = (List<Object>)classDefine.newInstance();
			list = newInstance;
        }
        if(classDefine!=null || method != null){
        	Class<?> newClass = ClassUtil.getClassDefine(method,rootClassDefine,index+1);
			if(newClass==null){
				return null;
			}else{
                if(List.class.isAssignableFrom(newClass)){
		            NodeList nodes = getElementsCurrByTagName(element,name);
		            if(nodes != null && nodes.getLength() > 0){
		            	Element ele = ((Element)nodes.item(valueIndex));
		            	nodes = getElementsCurrByTagName(ele,"list");
		            	if(nodes != null && nodes.getLength() > 0){
				            for(int i = 0; i < nodes.getLength(); i++)
				            {
				            	Object value = readListValue(ele, newClass,rootClassDefine, method,index+1,"list",i);
				            	list.add(value);
				            }
		            	}
		            }
                }else if(Map.class.isAssignableFrom(newClass)){
		            NodeList nodes = getElementsCurrByTagName(element,name);
		            if(nodes != null && nodes.getLength() > 0){
		            	Element ele = ((Element)nodes.item(valueIndex));
		            	nodes = getElementsCurrByTagName(ele,"map");
		            	if(nodes != null && nodes.getLength() > 0){
				            for(int i = 0; i < nodes.getLength(); i++)
				            {
				            	Object value = readMapValue(ele, newClass, rootClassDefine,method,index+1,"map",i);
				            	list.add(value);
				            }
		            	}
		            }
                }else if(isCustomClass(newClass)){
		            NodeList nodes = getElementsCurrByTagName(element,name);
		            if(nodes != null && nodes.getLength() > 0){
		            	Element ele = ((Element)nodes.item(valueIndex));
		            	nodes = getElementsCurrByTagName(ele,"value");
		            	if(nodes != null && nodes.getLength() > 0){
				            for(int i = 0; i < nodes.getLength(); i++)
				            {
			                    Object value = readValue(((Element)nodes.item(i)), newClass,rootClassDefine,method);
			                    list.add(value);
				            }
		            	}
		            }
	        	}else{
		            NodeList nodes = getElementsCurrByTagName(element,name);
		            if(nodes != null && nodes.getLength() > 0){
		            	Element ele = (Element)nodes.item(valueIndex);
		            	nodes = getElementsCurrByTagName(ele,"value");
		            	if(nodes != null && nodes.getLength() > 0){
				            for(int i = 0; i < nodes.getLength(); i++)
				            {
				            	String tValue = getValue(ele,"value",i);
				            	if(tValue!=null){
				            		list.add(convertValue(newClass,tValue));
				            	}
				            }
		            	}
		            }
	            }
        	}
        }
        if(!list.isEmpty())
            rtn = list;
        return rtn;
    }
	
    /**
     * 读取list中的值
     * @param element DOM的Element元素
     * @param classDefine class定义
     * @param rootClassDefine 多个rootClassDefine
     * @param method 方法定义
     * @param indexElement的名称
     * @param name Element的名称
     * @param valueIndex读取值的索引
     * @return Object 返回实例
     * @throws IllegalArgumentException
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws ClassNotFoundException
     * @throws ParseException
     */
	@SuppressWarnings("unchecked")
	private Map<Object,Object> readMapValue(Element element, Class<?> classDefine,Class<?>[] rootClassDefine,Method method,int index,String name,int valueIndex)
        throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, ParseException
    {
		Map<Object,Object> rtn = null;
		Map<Object,Object> map = null;
        if(classDefine.isInterface()){
        	Class<?> newClass = ClassUtil.getClassDefine(method,rootClassDefine,index);
			if(newClass!=null && !newClass.isInterface() && Map.class.isAssignableFrom(newClass)){
				map = (Map<Object,Object>)newClass.newInstance();
			}else {
				map = new HashMap<Object,Object>();
			}
        }else{
        	Map<Object,Object> newInstance = (Map<Object,Object>)classDefine.newInstance();
        	map = newInstance;
        }
        if(classDefine!=null || method != null){
        	Class<?> keyClass = ClassUtil.getClassDefine(method,rootClassDefine,index+1);
        	List<Object> keys = new ArrayList<Object>();
        	List<Object> values = new ArrayList<Object>();
			if(keyClass==null){
				return null;
			}else{
	            NodeList nodes = getElementsCurrByTagName(element,name);
	            if(nodes != null && nodes.getLength() > 0){
	            	Element ele = (Element)nodes.item(valueIndex);
	            	nodes = getElementsCurrByTagName(ele,"entry");
	            	if(nodes != null && nodes.getLength() > 0){
			            for(int i = 0; i < nodes.getLength(); i++)
			            {
			            	String tValue = getValue((Element)nodes.item(i),"key",0);
			            	if(tValue!=null){
			            		keys.add(convertValue(keyClass,tValue));
			            	}
			            }
	            	}
	            }
        	}
			if(!keys.isEmpty()){
	        	Class<?> valueClass = ClassUtil.getClassDefine(method,rootClassDefine,index+2);
				if(valueClass==null){
					return null;
				}else{
	                if(List.class.isAssignableFrom(valueClass)){
			            NodeList nodes = getElementsCurrByTagName(element,name);
			            if(nodes != null && nodes.getLength() > 0){
			            	Element ele = ((Element)nodes.item(valueIndex));
			            	nodes = getElementsCurrByTagName(ele,"entry");
			            	if(nodes != null && nodes.getLength() > 0){
					            for(int i = 0; i < nodes.getLength(); i++)
					            {
					            	Object value = readListValue((Element)nodes.item(i), valueClass,rootClassDefine, method,index+2,"value",0);
					            	values.add(value);
					            }
			            	}
			            }
	                }else if(Map.class.isAssignableFrom(valueClass)){
			            NodeList nodes = getElementsCurrByTagName(element,name);
			            if(nodes != null && nodes.getLength() > 0){
			            	Element ele = ((Element)nodes.item(valueIndex));
			            	nodes = getElementsCurrByTagName(ele,"entry");
			            	if(nodes != null && nodes.getLength() > 0){
					            for(int i = 0; i < nodes.getLength(); i++)
					            {
					            	Object value = readMapValue((Element)nodes.item(i), valueClass,rootClassDefine, method,index+2,"value",0);
					            	values.add(value);
					            }
			            	}
			            }
	                }else if(isCustomClass(valueClass)){
			            NodeList nodes = getElementsCurrByTagName(element,name);
			            if(nodes != null && nodes.getLength() > 0){
			            	Element ele = ((Element)nodes.item(valueIndex));
			            	nodes = getElementsCurrByTagName(ele,"entry");
			            	if(nodes != null && nodes.getLength() > 0){
					            for(int i = 0; i < nodes.getLength(); i++)
					            {
					            	NodeList valueNodes = getElementsCurrByTagName((Element)nodes.item(i),"value");
				                    Object value = readValue((Element)valueNodes.item(0), valueClass,rootClassDefine,method);
				                    values.add(value);
					            }
			            	}
			            }
		        	}else{
			            NodeList nodes = getElementsCurrByTagName(element,name);
			            if(nodes != null && nodes.getLength() > 0){
			            	Element ele = (Element)nodes.item(valueIndex);
			            	nodes = getElementsCurrByTagName(ele,"entry");
			            	if(nodes != null && nodes.getLength() > 0){
					            for(int i = 0; i < nodes.getLength(); i++)
					            {
					            	String tValue = getValue((Element)nodes.item(i),"value",0);
					            	if(tValue!=null){
					            		values.add(convertValue(keyClass,tValue));
					            	}
					            }
			            	}
			            }
		            }
	        	}
				for(int i=0;i<keys.size();i++){
					map.put(keys.get(i), values.get(i));
				}
			}
        }
        if(!map.isEmpty())
            rtn = map;
        return rtn;
    }
    

    /**
     * 将xml转义后的字符串转化成正常的字符串
     * @param xml xml字符
     * @param unicode 是否使用unicode
     * @return String 转化后的字符
     */
    public static final String format(String xml,boolean unicode){
    	if(unicode){
    		return StringUtil.decodeUnicode(xml);
    	}else{
    		return xml;
    	}
    }
}